package thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class DiedReentrantLock  extends Thread {
    static ReentrantLock reentrantLock1 = new ReentrantLock(true);

    static ReentrantLock reentrantLock2 = new ReentrantLock(true);

    int i = 1;

    public static void main(String[] args) {
        DiedReentrantLock diedReentrantLock = new DiedReentrantLock(1, "zhangsan");
        DiedReentrantLock diedReentrantLock1 = new DiedReentrantLock(2, "lisi");
        diedReentrantLock.start();
        diedReentrantLock1.start();



    }

    public DiedReentrantLock(int i,String name) {
        super(name);
        this.i = i;
    }

    @Override
    public void run() {
        try {


       if (i==1){
           reentrantLock1.lockInterruptibly();
           try {
               TimeUnit.SECONDS.sleep(1);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           reentrantLock2.tryLock(1,TimeUnit.MINUTES);

       }else if (i==2){
           reentrantLock2.tryLock(1,TimeUnit.MINUTES);
           try {
               TimeUnit.SECONDS.sleep(1);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           reentrantLock1.tryLock(1,TimeUnit.MINUTES);
       }
       if (reentrantLock1.isHeldByCurrentThread()){
           reentrantLock1.unlock();
       }
       if (reentrantLock2.isHeldByCurrentThread()){
           reentrantLock2.unlock();
       }
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {

        }
    }
}
